$(function () {
    layui.use(['element', 'layer', 'form', 'upload'], function () {
        var $ = layui.jquery, element = layui.element, layer = layui.layer, form = layui.form, upload = layui.upload;
        $('.save-info-btn').on('click', function () {
            saveInfo(layer)
        })
        //执行实例
        var uploadInst = upload.render({
            elem: '#upload-icon-btn',
            url: '/student/uploadIcon', //上传接口
            accept: 'images',
            acceptMime: 'image/*',
            size: 1024,
            method: 'post',
            done: function (res) {
                console.log(res)
                location.reload()
            },
            error: function () {
                //请求异常回调
                console.log('error')
            }
        });
    });
    // 上传头像
    $('.bg .base-info .logo .img, .bg .base-info .logo .upload-icon').hover(function () {
        $('.bg .base-info .logo .img').addClass('upload-back-img')
        $('.bg .base-info .logo .upload-icon').show()
    }, function () {
        $('.bg .base-info .logo .img').removeClass('upload-back-img')
        $('.bg .base-info .logo .upload-icon').hide()
    })
})

// 保存个人基本信息
function saveInfo(layer) {
    var nickName = $.trim($('#nickName').val())
    var userName = $.trim($('#userName').val())
    var password = $.trim($('#password').val())
    var sex = $('input[name="sex"]:checked').val()
    var phone = $.trim($('#phone').val())
    var city = $.trim($('#city').val())
    var company = $.trim($('#company').val())
    var address = $.trim($('#address').val())
    var sign = $.trim($('#sign').val())
    $.ajax({
        url: '/student/updateSingle',
        type: 'post',
        data: {
            stuName: userName,
            nickName: nickName,
            password: password,
            stuSex: sex,
            stuMobile: phone,
            city: city,
            sign: sign,
            stuAddress: address
        },
        dataType: 'json',
        success: function (response) {
            if (response.code === '000403') {
                console.log('未登录')
            } else if (response.code === '000000') {
                layer.msg('更新成功，请重新登录...', {icon: 1, time: 1000}, function () {
                    $.ajax({
                        url: '/student/logout',
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            if (data.code === '000000') {
                                location.reload();
                            } else {
                                console.log('退出系统异常')
                            }
                        }
                    })
                });
            } else {
                layer.msg(response.message)
            }
        }
    })
}